package com.ququ.syb.commission.pubs;

import com.ququ.common.exception.QuQuException;
import com.ququ.common.result.ResultJson;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.ququ.syb.commission.pubs.ApiConfig.API_BASE_URL;

@Component
public class PublicServiceApi {

    private Logger logger = LoggerFactory.getLogger(PublicServiceApi.class);
    @Autowired
    private ApiClient apiClient;

    /**
     * 发送短信
     * @param mobile
     * @param signcode
     * @param templatecode
     * @param json
     * @return
     */
    public boolean sendSms(String mobile,String signcode,String templatecode,String json){
        Map<String,Object> params = new HashMap<>();
        params.put("mobile",mobile);
        params.put("signcode",signcode);
        params.put("templatecode",templatecode);
        params.put("json",json);
        ResultJson result = apiClient.ApiClient(API_BASE_URL+ApiConfig.API_SEND_SMS,params);
        if (result.getStatus() == 1)
            return true;
        else
            return false;
    }


    /**
     * 发送语音验证码
     * @param mobile
     * @param templatecode
     * @param json
     * @return
     */
    public boolean sendVoice(String mobile,String templatecode,String json){
        Map<String,Object> params = new HashMap<>();
        params.put("mobile",mobile);
        params.put("templatecode",templatecode);
        params.put("json",json);
        ResultJson result = apiClient.ApiClient(API_BASE_URL+ApiConfig.API_SEND_VOICE,params);
        if (result.getStatus() == 1)
            return true;
        else
            return false;
    }


    /**
     * 获取银行卡头信息
     * @param bankcardno
     * @return
     */
    public Map<String,String> getCardInfo(String bankcardno){
        Map<String,Object> params = new HashMap<>();
        params.put("bankcardno",bankcardno);
        ResultJson result = apiClient.ApiClient(API_BASE_URL+ApiConfig.API_CARD_INFO,params);
        if (result.getStatus() != 1)
            throw new QuQuException(result.getStatus(),result.getMsg());
        else
            return (Map<String, String>) result.getData();
    }


    /**
     * 三四要素验证
     * @param name
     * @param bankcardno
     * @param idcardno
     * @param mobile
     * @return
     */
    public Map<String,String> verifyBankCard(String name,
                                             String bankcardno,
                                             String idcardno,
                                             String mobile){
        Map<String,Object> params = new HashMap<>();
        params.put("name",name);
        params.put("bankcardno",bankcardno);
        params.put("idcardno",idcardno);
        Map<String,String> resultMap = null;
        if (StringUtils.isNotBlank(mobile)){
            params.put("mobile",mobile);
            resultMap = (Map<String, String>) apiClient.apiClient(API_BASE_URL+ApiConfig.API_CARD_VERIFY4,params);
        }else
            resultMap = (Map<String, String>) apiClient.apiClient(API_BASE_URL+ApiConfig.API_CARD_VERIFY3,params);
        return resultMap;
    }

    public boolean isWorkingday(String day){
        Map<String,Object> params = new HashMap<>();
        params.put("day",day);
        ResultJson result = apiClient.ApiClient(API_BASE_URL+ApiConfig.API_ISWORKINGDAY,params);
        if (result.getStatus() != 1)
            throw new QuQuException(result.getStatus(),result.getMsg());
        else {
            Map<String,Object> resultMap =  (Map<String, Object>) result.getData();
            return Integer.parseInt(resultMap.get("isWorkingDay").toString())==1;
        }
    }


    public String nextWorkingday(String day){
        Map<String,Object> params = new HashMap<>();
        params.put("day",day);
        ResultJson result = apiClient.ApiClient(API_BASE_URL+ApiConfig.API_NEXTWORKINGDAY,params);
        if (result.getStatus() != 1)
            throw new QuQuException(result.getStatus(),result.getMsg());
        else {
            Map<String,Object> resultMap =  (Map<String, Object>) result.getData();
            return resultMap.get("nextWorkingDay").toString();
        }
    }


    public String lastWorkingday(String day){
        Map<String,Object> params = new HashMap<>();
        params.put("day",day);
        ResultJson result = apiClient.ApiClient(API_BASE_URL+ApiConfig.API_LASTWORKINGDAY,params);
        if (result.getStatus() != 1)
            throw new QuQuException(result.getStatus(),result.getMsg());
        else {
            Map<String,Object> resultMap =  (Map<String, Object>) result.getData();
            return resultMap.get("lastWorkingDay").toString();
        }
    }


}
